using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CodePlex.SpaceBlock.FS;
using CodePlex.Resourceful;

namespace CodePlex.SpaceBlock.UI.Controls.Log
{
    public partial class LogControl : CodePlex.SpaceBlock.UI.Controls.BaseControl
    {
        private readonly InnerListener _innerListener;

        public LogControl()
        {
            InitializeComponent();

            this.CreateHandle();    // so we don't miss any messages generated before the control is visible
     
            _innerListener = new InnerListener(this);
        }


        public void Initialize(FileSystemManager manager)
        {
            if (!manager.IsValid)
            {
                return;
            }
            foreach (IFileSystem fs in manager.FileSystems)
            {
                if (fs is S3FileSystem 
                    || fs is NirvanixFileSystem 
                    || fs is SSDSFileSystem 
                    || fs is AzureBlobStorageFileSystem
                    || fs is SunCloudFileSystem)
                {
                    if (!fs.Tracer.Listeners.Contains(_innerListener))
                    {
                        fs.Tracer.Listeners.Add(_innerListener);
                    }

                }
            }
        }

        private class InnerListener : TracerListener
        {
            private readonly LogControl _parent;

            public InnerListener(LogControl parent)
            {
                _parent = parent;
            }

            public override void WriteLine(string message)
            {
                _parent._logBox.WriteLine(message);

            }

            public override void Write(string value)
            {
                _parent._logBox.Write(value);
            }
        }

       

        private void _wordWrapCheck_CheckedChanged(object sender, EventArgs e)
        {
            _logBox.WordWrap = _wordWrapCheck.Checked;
        }

        private void _clearButton_Click(object sender, EventArgs e)
        {
            _logBox.Clear();
        }

    }
}

